<?php
require 'runtime.php';

$do = isset($_GET['do']) ? trim($_GET['do']) : "";
if($do == 'getinfo'){

	$db = init_db();
	
	$row = array();
		
	// 服务器状态
	exec("uptime", $s);
	$arr = explode("average:", $s[0]);
	$arr1 = explode(",", $arr[1]);
	$row[0] = array($s[0], str_replace(",", "", $arr1[0]));
		
	$rs = $db->get_rows("select id,content,num,lastupdate,lastcity,bytes_grab,bytes_update,hidden from weather_server order by id");
	$server_list = '';
	foreach($rs as $r){
		$r["bytes"] = byte_format($r["bytes_grab"] + $r["bytes_update"]);
		$row[] = $r;
	}
	
	$rs = $db->get_rows("select * from weather_history order by lastupdate desc limit 0,8");
	$i = sizeof($row);
	$row[] = array();
	foreach($rs as $r){
		$row[$i][] = $r;
	}
	
	$rs = $db->get_rows("select id,cityid,province,city,lastupdate,lastsync,num,type from weather_data order by lastsync desc limit 8");
	$i = sizeof($row);
	$row[] = array();
	foreach($rs as $r){
		$r["lastsync"] = date("H:i:s", strtotime($r["lastsync"]));
		$row[$i][] = $r;
	}
	
	// 城市更新总量
	$i = sizeof($row);
	$row[] = array();
	$rs = $db->get_row("select sum(num) as num from weather_data");
	$row[$i][] = $rs["num"];

	ob_start('ob_gzip');
	echo json_encode($row);
	ob_end_flush(); //结束缓冲区，输出内容。
	
}else{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<link rel="apple-touch-icon" href="/images/apple-touch-icon-iphone.png" />
<link rel="apple-touch-icon" sizes="72x72" href="/images/apple-touch-icon-ipad.png" />
<link rel="apple-touch-icon" sizes="114x114" href="/images/apple-touch-icon-iphone4.png" />
<meta name="viewport" content="width=480" />
<title>分流服务器使用记录</title>
<script type="text/javascript" src="/scripts/jquery.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.formatnum.js"></script>
<style type="text/css">
a {blr:expression(this.onFocus=this.blur())}
body{font-size: 12px;	margin:2px;background-color:#ffffff;}
table{border-collapse:collapse;border: 1px solid #cccccc;}
td{font-size: 12px;border-collapse:collapse;border: 1px solid #cccccc;}
</style>
</head>
<body>
<table width="476" border="1" cellspacing="1" cellpadding="2">
  <tbody>
    <tr>
      <td id="status">Loading ...</td>
    </tr>
  </tbody>
</table>
<table width="476" border="1" cellspacing="1" cellpadding="2" style="margin-top:6px;">
  <tbody>
    <tr>
      <td bgcolor="#F0F0F0" height="22" align="center"><strong><a href="/shell/auto_clear.php" target="_blank">编号</a></strong></td>
      <td bgcolor="#F0F0F0"><strong>服务器说明</strong></td>
      <td bgcolor="#F0F0F0" align="center"><strong>更新时间</strong></td>
      <td bgcolor="#F0F0F0" align="center"><strong>更新</strong></td>
      <td bgcolor="#F0F0F0" align="center"><strong>数据流量</strong></td>
      <td width="50" align="center" bgcolor="#F0F0F0"><strong>城市</strong></td>
    </tr>
  </tbody>
  <tbody id="info"><tr><td colspan="6">Loading ...</td></tr></tbody>
</table>
<table width="476" border="1" cellspacing="1" cellpadding="2" style="margin-top:6px;">
  <tbody>
    <tr>
      <td height="22" bgcolor="#F0F0F0"><strong>更新服务器</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>城市代码</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>类型</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>城市</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>更新时间</strong></td>
    </tr>
  </tbody>
    <tbody id="logs"><tr><td colspan="5">Loading ...</td></tr></tbody>
</table>
<table width="476" border="1" cellspacing="1" cellpadding="2" style="margin-top:6px;">
  <tbody>
    <tr>
      <td height="22" align="center" bgcolor="#F0F0F0"><strong>城市代码</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>城市名称</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>类型</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>更新次数</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>天气同步</strong></td>
      <td align="center" bgcolor="#F0F0F0"><strong>设备同步</strong></td>
    </tr>
  </tbody>
  <tbody id="logs2">
    <tr>
      <td colspan="6">Loading ...</td>
    </tr>
  </tbody>
</table>
<script type="text/javascript">
var flow_total = 0;
var flow_add = 0;
function GetData(){
	$.ajax({
		url: "/s1.js",
		cache: false,
		dataType: "json",
		success: function(s){
			var r = "";
			var nums = 0;
			var flow = 0;
			var servernum = 0;
			$("#status").html(" &nbsp;Server:"+s[0][0]);
			if(s[0][1]>=1 && s[0][1]<2) { $("#status").css({"background-color":"yellow", "color":"#000000"}); }
			else if (s[0][1]>=2) { $("#status").css({"background-color":"red", "color":"white"}); }
			else { $("#status").css({"background-color":"#ccff99", "color":"#000000"}); }
			for(i=1; i<s.length-3; i++){
				if(s[i]["hidden"]=="0"){
					r += "<tr title=\"加入时间：" + s[i]["join"] + "\"><td align=\"center\" height=\"16\">" + s[i]["id"] + "</td>";
					r += "<td>" + s[i]["content"] + "</td>";
					r += "<td align=\"center\">" + s[i]["lastupdate"] + "</td>";
					r += "<td align=\"center\">" + s[i]["num"] + "</td>";
					r += "<td align=\"center\">" + s[i]['bytes'] + "</td>";
					r += "<td align=\"center\">" + s[i]["lastcity"] + "</td></tr>";
					servernum += 1;
				}
				nums += Number(s[i]["num"]);
				flow += Number(s[i]['bytes_grab']) + Number(s[i]['bytes_update']);
			}
			if(flow-flow_total!=0){
				flow1 = flow_total>0 ? flow-flow_total : 0;
				flow_add = flow1;
			}else{
				flow1 = 0;
			}			
			
			flow_total = flow;
			r += "<tr><td colspan=\"6\" height=\"16\">服务器数：" + servernum + " 更新总数：" + $.formatNum(nums) + " 总流量：<span id=\"total\" style=\"color:blue\">" + $.formatNum(flow) + "</span> Bytes + <span style=\"color:red\">" + $.formatNum(flow1) + "</td>";
			$("#info").html(r);
			
			r = "";
			for(i=0; i<s[s.length-3].length; i++){
				r += "<tr><td>" + s[s.length-3][i]['server'] + "</td><td align=\"center\">" + s[s.length-3][i]['citycode'] + "</td><td align=\"center\">" + s[s.length-3][i]['type'] + "</td><td align=\"center\">" + s[s.length-3][i]['city'] + "</td><td align=\"center\">" + s[s.length-3][i]['lastupdate'] + "</td></tr>";
			}
			$("#logs").html(r);
			
			r = "";
			for(i=0; i<s[s.length-2].length; i++){
				r += "<tr><td align=\"center\">" + s[s.length-2][i]['cityid'] + "</td><td align=\"center\">" + s[s.length-2][i]['city'] + "</td><td align=\"center\">" + s[s.length-2][i]['type'] + "</td><td align=\"center\">" + s[s.length-2][i]['num'] + "</td><td align=\"center\">" + s[s.length-2][i]['lastupdate'] + "</td><td align=\"center\">" + s[s.length-2][i]['lastsync'] + "</td></tr>";
			}
			r += "<tr><td colspan=\"6\">客户端更新数量：" + $.formatNum(s[s.length-1]) + " 缓存命中率：" + s[s.length-1]/nums + "</td></tr>";
			$("#logs2").html(r);
		}
	});
}
$(document).ready(function(){setInterval(function (){GetData()}, 1000);});
</script>
</body>
</html>
<?php
}

function ob_gzip($content){ // $content 就是要压缩的页面内容，或者说饼干原料
	if(!headers_sent() && // 如果页面头部信息还没有输出
	extension_loaded("zlib") && // 而且zlib扩展已经加载到PHP中
	strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")){ //而且浏览器说它可以接受GZIP的页面 
		$content = gzencode($content,9); // 用zlib提供的gzencode()函数执行级别为9的压缩，这个参数值范围是0-9，0表示无压缩，9表示最大压缩，当然压缩程度越高越费CPU。
		//然后用header()函数给浏览器发送一些头部信息，告诉浏览器这个页面已经用GZIP压缩过了！ 
		header("Content-Encoding: gzip"); 
		header("Vary: Accept-Encoding");
		header("Content-Length: ".strlen($content));
	}
	return $content; //返回压缩的内容，或者说把压缩好的饼干送回工作台。
}
?>